package com.yuzhi.common.translation.impl;


import com.yuzhi.common.annotation.TranslationType;
import com.yuzhi.common.contant.TransConstant;
import com.yuzhi.common.core.service.UserService;
import com.yuzhi.common.translation.TranslationInterface;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 用户名翻译实现
 *
 * @author auto
 */
@Component
@AllArgsConstructor
@TranslationType(type = TransConstant.USER_ID_TO_NAME)
public class UserNameTranslationImpl implements TranslationInterface<String> {

    private final UserService userService;

    @Override
    public String translation(Object key, String other) {
        if (key instanceof String) {
            String userIds = (String) key;
            // 将逗号分隔的字符串转换为列表
            List<String> userIdList = Arrays.asList(userIds.split(","));
            // 调用 userService 方法处理每个 ID，并将结果拼接起来
            return Optional.ofNullable(userIdList)
                    .orElse(Collections.emptyList())
                    .stream()
                    .map(userService::selectNickNameById)
                    .filter(Objects::nonNull)
                    .collect(Collectors.joining(", "));
        }
        return null;
    }
}
